
/***********************************************************************
 * Planet class
 * 
 * Implements the planet artifact
 *
 * Signature:
 *  public static Planet create (String name, Coord position);
 *  public void install ();
 *  public void destroy ();
 *  public Coord position ();
 *  public String name ();
 *  public void report (String message);
 *  public void changePosition (Coord target);
 *  public boolean isOrbitable ();
 *  public void takeDamage (int damage);
 *  public abstract char display ();
 *  public abstract String toString ();
 ***********************************************************************/

class Planet extends Artifact {

  protected Planet (String n, Coord p) {
    super(n,p);
  }

  public static Planet create (String n, Coord p) {
    Planet pl = new Planet(n,p);
    pl.install();
    return pl;
  }

  public char display () {
    return 'o';
  }

  public String toString () {
    return "Planet " + this.name() + " " + this.position().toString();
  }

  public boolean isOrbitable () {
    return true;
  }

  public String jsonString() {
    return "{Planet:[" + name() + "," + position() + "]}";
  }

  public Artifact fromJSONString(String jsonString) {
    String fields[] = JSON.decodeJSONArray(jsonString);
    if(fields.length != 2) {
      throw new RuntimeException(JSON.malformedErrorString("Planet",
							   jsonString));
    }

    try {
      return this.create(fields[0], 
			 Coord.create(0,0).fromJSONString(fields[1]));
    } catch (RuntimeException re) {
      System.err.println("Following error encountered while parsing a"
			 + " Planet json string. (" + jsonString + ")");
      throw re;
    }
  }
}
